﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manager_Sanpham_Controls_UC_Suasanpham : System.Web.UI.UserControl
{
    BUS_Product data = new BUS_Product();
    BUS_Manufacturer data2 = new BUS_Manufacturer();
    DTO_Product obj = new DTO_Product();
    BUS_GroupProduct group = new BUS_GroupProduct();
    string view = "";
    string order = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hplXemsanpham.NavigateUrl = "../Xemsanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"] + "&id_sp=" + Request.QueryString["id_sp"];
            hplSuasanpham.NavigateUrl = "../Suasanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"] + "&id_sp=" + Request.QueryString["id_sp"];
            string id_sp = Request.QueryString["id_sp"].ToString();
            var dt = data.Product_Detail(id_sp);
            DropDownList1.DataSource = data2.Manufacturer_GetByAll();
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
            DropDownList1.SelectedValue = Request.QueryString["id_nsx"];
            dlLoaiSP.DataSource = group.GroupProduct_GetByAll();
            dlLoaiSP.DataBind();


            txtTensanpham.Text = dt.Rows[0]["TenSP"].ToString();

            CK.config.toolbar = new object[] { new object[] { "Bold", "Italic", "-", "NumberedList", "BulletedList","-", "Outdent", "Indent", "-", "Blockquote", "CreateDiv","-", "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock", "-", "BidiLtr", "BidiRtl", "-", "Link", "Unlink", "-" },
                            new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, };

            CK.Text = dt.Rows[0]["Chitiet"].ToString();
            txtGia.Text = dt.Rows[0]["Gia"].ToString();
            Image1.ImageUrl = dt.Rows[0]["Hinhanh"].ToString();
            //Image1.ImageUrl = "~/Images/product/casio-logo.png";
            bool status = bool.Parse(dt.Rows[0]["Trangthai"].ToString());
            if (status)
            {
                RdMo.Checked = true;
            }
            else
            {
                RdDong.Checked = true;
            }
            view = dt.Rows[0]["Hienthi"].ToString();
            order = dt.Rows[0]["Thutu"].ToString();
        }
        
    }
    public bool Mo(bool Status)
    {
        if (Status)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool Dong(bool Status)
    {
        if (Status)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    protected void btnSuasanpham_Click(object sender, EventArgs e)
    {
        string id_sp = Request.QueryString["id_sp"].ToString();
        //Response.Write("bvjh"+Repeater1.FindControl("TextBox1"));
        int a = int.Parse(DropDownList1.SelectedValue);
        //string a = DropDownList1.SelectedItem.Value;
        obj.Manufacturer_Id = a;
        obj.GroupProduct_Id = Group();
        obj.Name = txtTensanpham.Text;
        obj.Detail = CK.Text;
        obj.Price = txtGia.Text;
        if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(Server.MapPath("~/Upload/Product/" + FileUpload1.FileName));
                obj.Image = "~/Upload/Product/" + FileUpload1.FileName;
            }
        else if (txtUrlIamge.Text.Length>0)
        {
            obj.Image = txtUrlIamge.Text;
        }
        else
        {
            obj.Image = Image1.ImageUrl.ToString();
        }
        //obj.View = int.Parse(view);
        //obj.Order = int.Parse(order);
        if (RdMo.Checked)
        {
            obj.Status = true;
        }
        else
        {
            obj.Status = false;
        }
        obj.Date = string.Format("{0:G}",DateTime.Now);

        data.Product_UpdateByAll(id_sp, obj);

        Response.Redirect(Request.RawUrl);
    }
    public string Thongbao(string text)
    {
        return "<script>alert('" + text + "')</script>";
    }

    public bool GetGroup(string idGroup)
    {
        string id_sp = Request.QueryString["id_sp"].ToString();
        var dt = data.Product_GetByTop(0, "Id = '" + id_sp + "' and GroupProduct_Id like '%" + idGroup + "%'", "");
        if (dt.Rows.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public string Group()
    {
        string li = "";
        foreach (DataListItem item in dlLoaiSP.Items)
        {

            CheckBox linh = (CheckBox)item.FindControl("CheckBox1");
            if (linh.Checked)
            {
                TextBox tb = (TextBox)item.FindControl("TextBox1");
                li += tb.Text + "|";
            }
        }
        return li;
    }
    
}